package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.ProductBean;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {
    @Autowired
    private IProductService service;

    @RequestMapping("findByItem")
    public String  findByItem(
            @RequestParam(value = "pageNO",defaultValue = "1") int pageNO,
            String name, LocalDate startDate, LocalDate endDate,
            HttpServletRequest request){
        PageInfo<ProductBean> info = service.findByItem(pageNO,name,startDate,endDate);
        request.setAttribute("name",name);
        request.setAttribute("startDate",startDate);
        request.setAttribute("endDate",endDate);
        request.setAttribute("pageInfo",info);

        return "/html/product/product.html";
    }

    @RequestMapping("/add")
    public String add(ProductBean product){
        service.add(product);

        return "redirect:/thymeleaf/findByItem";
    }

    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return "redirect:/thymeleaf/findByItem";
    }

    @RequestMapping("findById")
    public String findById(int id,HttpServletRequest request){
        //调用业务方法，查询商品
        ProductBean product = service.findById(id);
        request.setAttribute("product",product);
        return "/html/product/update.html";
    }

    @RequestMapping("update")
    public String update(int id,int price){
        service.update(id,price);
        return "redirect:/thymeleaf/findByItem";
    }
}
